package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.ProductCategoryDTO;
import com.zenithmind.mall.pojo.vo.ProductCategoryVO;
import com.zenithmind.mall.service.ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/category")
@RequiredArgsConstructor
@Tag(name = "商品分类管理", description = "商品分类相关接口")
public class ProductCategoryController {

    private final ProductCategoryService categoryService;

    @GetMapping("/tree")
    @Operation(summary = "获取分类树", description = "获取完整的分类树结构")
    public Result<List<ProductCategoryVO>> getCategoryTree() {
        log.info("获取商品分类树");
        List<ProductCategoryVO> result = categoryService.getCategoryTree();
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有启用分类", description = "获取所有启用状态的分类列表")
    public Result<List<ProductCategoryVO>> getEnabledCategories() {
        log.info("获取所有启用分类");
        List<ProductCategoryVO> result = categoryService.getEnabledCategories();
        return Result.success(result);
    }

    @GetMapping("/children/{parentId}")
    @Operation(summary = "获取子分类", description = "根据父分类ID获取子分类列表")
    @Parameter(name = "parentId", description = "父分类ID", required = true)
    public Result<List<ProductCategoryVO>> getCategoriesByParentId(@PathVariable String parentId) {
        log.info("获取子分类，父分类ID: {}", parentId);
        List<ProductCategoryVO> result = categoryService.getCategoriesByParentId(parentId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取分类详情", description = "根据分类ID获取详细信息")
    @Parameter(name = "id", description = "分类ID", required = true)
    public Result<ProductCategoryVO> getCategoryById(@PathVariable String id) {
        log.info("获取分类详情: {}", id);
        ProductCategoryVO result = categoryService.getCategoryById(id);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建分类", description = "创建新的商品分类")
    @RequireAuth(admin = true)
    @Log(description = "创建商品分类", operationType = OperationTypeEnum.ADD)
    public Result<String> createCategory(@Validated @RequestBody ProductCategoryDTO categoryDTO) {
        log.info("创建商品分类: {}", categoryDTO.getName());
        String categoryId = categoryService.createCategory(categoryDTO);
        return Result.success(categoryId);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新分类", description = "更新商品分类信息")
    @Parameter(name = "id", description = "分类ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "更新商品分类", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateCategory(@PathVariable String id, 
                                        @Validated @RequestBody ProductCategoryDTO categoryDTO) {
        log.info("更新商品分类: {}", id);
        Boolean result = categoryService.updateCategory(id, categoryDTO);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除分类", description = "删除商品分类")
    @Parameter(name = "id", description = "分类ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "删除商品分类", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteCategory(@PathVariable String id) {
        log.info("删除商品分类: {}", id);
        Boolean result = categoryService.deleteCategory(id);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除分类", description = "批量删除商品分类")
    @RequireAuth(admin = true)
    @Log(description = "批量删除商品分类", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteCategoriesBatch(@RequestBody List<String> ids) {
        log.info("批量删除商品分类，数量: {}", ids.size());
        Boolean result = categoryService.deleteCategoriesBatch(ids);
        return Result.success(result);
    }
}
